package com.taoyi.mhouse.binder;
/**
 * created by tempo on 2015/11/13.
 */

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.taoy.recyclebinder.DataBindAdapter;
import com.taoy.recyclebinder.DataBinder;
import com.taoyi.mhouse.R;
import com.taoyi.mhouse.bean.Goods;
import com.taoyi.mhouse.util.UIUtils;
import com.tempo.widget.AutoLineLayout;
import com.tempo.widget.SectionBar;
import com.tempo.widget.SectionBarBottom;

import java.util.ArrayList;
import java.util.List;

public class GoodsBinder extends DataBinder<GoodsBinder.ViewHolder> {
    private int itemCount=1;
    public GoodsBinder(DataBindAdapter dataBindAdapter) {
        super(dataBindAdapter);
    }
    @Override
    public ViewHolder newViewHolder(ViewGroup parent) {
        View v = View.inflate(UIUtils.getContext(),R.layout.binder_recommend_goods, null);
        return new ViewHolder(v);
    }

    /**
     * 测试用
     * @param count
     */
    public void setItemCount(int count){
        itemCount=count;
    }
    @Override
    public void bindViewHolder(final ViewHolder holder, int position) {
        List<Goods>data=new ArrayList<Goods>();
        for(int i=0;i<4;i++){
            Goods goods=new Goods();
            goods.imgUrl = "";
            goods.name="灌篮高手"+i;
            goods.price=100;
            data.add(goods);
        }
       holder.setData(data);
        holder.mSectionBar.setOnSectionBarClickListtener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(UIUtils.getContext(), "see more clicked!", Toast.LENGTH_SHORT).show();
            }
        });
        holder.mSectionBottom.setOnRereshListener(new SectionBarBottom.OnRefreshListener() {
            @Override
            public void onRefresh() {
               holder.mSectionBottom.postDelayed(new Runnable() {
                   @Override
                   public void run() {
                       holder.mSectionBottom.stopRefresh();
                   }
               },2000);
            }
        });
    }
    @Override
    public int getItemCount() {
        return itemCount;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        public AutoLineLayout alGoods;
        private AutoAdapter  mAdapter;
        public  SectionBar mSectionBar;
        public SectionBarBottom mSectionBottom;
        public ViewHolder(View itemView) {
            super(itemView);
            alGoods = (AutoLineLayout) itemView.findViewById(R.id.al_goods);
            mAdapter=new AutoAdapter();
            alGoods.setAdapter(mAdapter);
            mSectionBottom= (SectionBarBottom) itemView.findViewById(R.id.mSectionBarBottom);
            mSectionBar = (SectionBar) itemView.findViewById(R.id.sectionbar);
        }
        public void setData(List<Goods> data){
          mAdapter.setData(data);
        }

    }
    static class AutoAdapter extends AutoLineLayout.AutoAdapter{
        private List<Goods> data;
        private List<Integer> imgs;
        public void setData(List<Goods> data){
            this.data=data;
            imgs=new ArrayList<>();
            imgs.add(R.drawable.toy1);
            imgs.add(R.drawable.toy2);
            imgs.add(R.drawable.toy3);
            imgs.add(R.drawable.toy4);
            notifyDataChaged();

        }
        @Override
        protected int getCount() {
            return data==null?0:data.size();
        }

        @Override
        protected Goods getData(int position) {
            return data.get(position);
        }

        @Override
        protected View getView(View parent, int position) {
            View v=View.inflate(UIUtils.getContext(),R.layout.item_goods,null);
            Holder holder=new Holder();
            holder.ivGoods = (ImageView) v.findViewById(R.id.iv_goods);
            holder.tvGoodsName= (TextView) v.findViewById(R.id.tv_goods_name);
            holder.tvGoodsPrice= (TextView) v.findViewById(R.id.tv_goods_price);
            v.setTag(holder);
            return v;
        }

        @Override
        protected void resetData(View child, int position) {
            Holder holder= (Holder) child.getTag();
            Goods goodsData=getData(position);
            holder.ivGoods.setImageResource(imgs.get(position));
            holder.tvGoodsName.setText(goodsData.name);
            holder.tvGoodsPrice.setText("price:"+goodsData.price);

        }
        private class Holder{
            public ImageView ivGoods;
            public TextView tvGoodsName;
            public TextView tvGoodsPrice;
        }
    }
}
